package org.martin.snsrs.crawler.database;

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for Tags
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see org.martin.snsrs.crawler.database.Tags
 * @author MyEclipse Persistence Tools
 */

public class TagsDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TagsDAO.class);
	// property constants
	public static final String TAG_DESCRIPT = "tagDescript";
	public static final String TAG_NAME = "tagName";

	public void save(Tags transientInstance) {
		log.debug("saving Tags instance");
		try {
			Transaction transaction = getSession().beginTransaction();
			getSession().save(transientInstance);
			transaction.commit();
			getSession().flush();
			getSession().close();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Tags persistentInstance) {
		log.debug("deleting Tags instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Tags findById(java.lang.Integer id) {
		log.debug("getting Tags instance with id: " + id);
		try {
			Tags instance = (Tags) getSession().get(
					"org.martin.snsrs.database.Tags", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Tags instance) {
		log.debug("finding Tags instance by example");
		try {
			List results = getSession()
					.createCriteria("org.martin.snsrs.database.Tags")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Tags instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Tags as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			List list = queryObject.list();
			if(list.isEmpty())
				return null;
			else
				return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			return null;
		}
	}

	public List findByTagDescript(Object tagDescript) {
		return findByProperty(TAG_DESCRIPT, tagDescript);
	}

	public List findByTagName(Object tagName) {
		return findByProperty(TAG_NAME, tagName);
	}

	public List findAll() {
		log.debug("finding all Tags instances");
		try {
			String queryString = "from Tags";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Tags merge(Tags detachedInstance) {
		log.debug("merging Tags instance");
		try {
			Tags result = (Tags) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Tags instance) {
		log.debug("attaching dirty Tags instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Tags instance) {
		log.debug("attaching clean Tags instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}